/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : WordAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 15, 2008       Raghu Sarraff			       Created
 */
package lwf.web.struts.action.english;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.english.WordHandler;
import lwf.web.struts.action.BaseAction;
import lwf.web.struts.action.form.english.WordDisplayActionForm;

import net.lwf.util.web.WebUtil;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


/**
 * Type the class description here
 *  
 */
//public abstract class WordAction extends DispatchAction {
public abstract class WordAction extends BaseAction {
	
	public abstract WordHandler getHandler(HttpServletRequest request, ActionForm form);
	public abstract void checkGrade(HttpServletRequest request, ActionForm form);
	public abstract int getDisplayRows(ActionForm form);
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WordDisplayActionForm swform = (WordDisplayActionForm) form;
		swform.setDisprows(0);
		WebUtil.removeAttribute(request, "end");
		WebUtil.removeAttribute(request, "start");
		checkGrade(request, form);
		return mapping.findForward("home");
	} //home	
	
	public ActionForward newdisplay(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		WordDisplayActionForm swform = (WordDisplayActionForm) form;
		WebUtil.removeAttribute(request, "end");
		WebUtil.removeAttribute(request, "start");
		return shownext(mapping, form, request, response);
	} //home
	
	public ActionForward shownext(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		processNext(request, form);
        return processResponse(form, request, response); // Not forwarding to anywhere, response is fully-cooked
	}
	
	public ActionForward showprevious(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		processPrevious(request, form);
        return processResponse(form, request, response);
	}
	
	public ActionForward display(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
        return processResponse(form, request, response);
	}
	
	//-------------------------------------------------------------------------------------
	// Private methods
	//-------------------------------------------------------------------------------------
	
	
	/**
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	private ActionForward processResponse(ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException {
		checkGrade(request, form);
        WordHandler handler = getHandler(request, form);
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(handler.getFormattedDisplay());
		out.flush();
		return null;
	}

	protected int getEnd(HttpServletRequest request) {
		String start = (String) WebUtil.getAttribute(request, "start");
		String end = (String) WebUtil.getAttribute(request, "end");
		if(start == null && end == null) {
			return 0;
		}
		else {
			return Integer.parseInt(end); //replace this value from the request
		}
	}
	
	protected int getStart(HttpServletRequest request) {
		String start = (String) WebUtil.getAttribute(request, "start");
		if(start == null) {
			return 0;
		}
		else {
			return Integer.parseInt(start); //replace this value from the request
		}
	}
	
	private void processNext(HttpServletRequest request, ActionForm form) {
        int start = 0;
        int end = getEnd(request);
        if(end != 0) {
        	start = end;
        }
        end = start + getDisplayRows(form);
        WebUtil.setAttribute(request, "start", String.valueOf(start));
        WebUtil.setAttribute(request, "end", String.valueOf(end));
	}
	
	private void processPrevious(HttpServletRequest request, ActionForm form) {
        int start = 0;
        int end = getStart(request);;
        if(end != 0) {
        	start = end - getDisplayRows(form);
        }
        if(start < 0) {
        	start = 0;
        }
        WebUtil.setAttribute(request, "start", String.valueOf(start));
        WebUtil.setAttribute(request, "end", String.valueOf(end));
	}

}

